/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package solitairetest;

import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import java.awt.dnd.*;
import java.awt.image.*;

import solitairetest.logic.*;

/**
 *
 * @author Administrator
 */
public class CardCanvas extends java.awt.Canvas
{
    public Pile pile;

    private Card card;
    private BufferedImage cards;

    private DragGestureListener dragGestureListener;
    private DragSource dragSource;
    private DragSourceListener dragSourceListener;
    private int dragAction = DnDConstants.ACTION_MOVE;

    public CardCanvas()
    {
        super();
        this.card = null;
        this.cards = null;
    }

    public CardCanvas(BufferedImage cards)
    {
        super();

        this.cards = cards;
        
        this.setMinimumSize(new Dimension(79, 123));
        this.setMaximumSize(new Dimension(79, 123));
        this.setSize(79, 123);

        this.setIgnoreRepaint(true);

        MouseHandler mouseHandler = new MouseHandler();
        this.addMouseListener(mouseHandler);

        this.dragSource = DragSource.getDefaultDragSource();
        this.dragGestureListener = new CardCanvasDragGestureListener();
        this.dragSourceListener = new CardCanvasDragSourceListener();

        this.dragSource.createDefaultDragGestureRecognizer(this, this.dragAction, this.dragGestureListener);
    }

    public CardCanvas(Card c, BufferedImage cards)
    {
        super();

        this.card = c;
        this.cards = cards;
        
        this.setMinimumSize(new Dimension(79, 123));
        this.setMaximumSize(new Dimension(79, 123));
        this.setSize(79, 123);

        MouseHandler mouseHandler = new MouseHandler();
        this.addMouseListener(mouseHandler);

        this.dragSource = DragSource.getDefaultDragSource();
        this.dragGestureListener = new CardCanvasDragGestureListener();
        this.dragSourceListener = new CardCanvasDragSourceListener();

        this.dragSource.createDefaultDragGestureRecognizer(this, this.dragAction, this.dragGestureListener);
    }

    public void paint(Graphics g)
    {
        super.paint(g);

        if (this.card != null)
        {
            int x = -1;
            int y = -1;

            if (this.card.GetRank() == 999)
            {
                y = 492;

                int suitNum = 0;
                for (int s = 0; s < Card.suits.length && suitNum == 0; s++)
                    if (Card.suits[s] == card.GetSuit())
                        suitNum = s;

                x = this.getMinimumSize().width * suitNum;
            }
            else if (this.card.GetFacing())
            {
                x = this.getMinimumSize().width * (card.GetRank() - 1);

                int suitNum = 0;
                for (int s = 0; s < Card.suits.length && suitNum == 0; s++)
                    if (Card.suits[s] == card.GetSuit())
                        suitNum = s;

                y = this.getMinimumSize().height * suitNum;
            }
            else
            {
                x = 316;
                y = 492;
            }

            if (x != -1 && y != -1)
                g.drawImage(this.cards, -x, -y, this);
        }
    }

    public Card SetCard(Card card)
    {
        this.card = card;

        return card;
    }

    public Card GetCard()
    {
        return card;
    }

    class MouseHandler extends MouseAdapter
    {
        int oldX = -1;
        int oldY = -1;

        public void mouseClicked(MouseEvent e)
        {
            if (e.getButton() == e.BUTTON1)
            {
                if (CardCanvas.this == CardCanvas.this.pile.pileCards.lastElement() && !CardCanvas.this.card.GetFacing())
                {
                    CardCanvas.this.card.SetFacing(true);
                    CardCanvas.this.repaint();
                }

                CardCanvas.this.pile.CardClicked(CardCanvas.this == CardCanvas.this.pile.pileCards.lastElement());
            }
        }
    }

    class CardCanvasDragGestureListener implements DragGestureListener
    {
        public void dragGestureRecognized(DragGestureEvent e)
        {
            if ((e.getDragAction() & CardCanvas.this.dragAction) == 0 || !CardCanvas.this.card.GetFacing())
                return;

            //Transferable transferable = new StringTransferable(CardCanvas.this.card.toString());

            Deck deck = new Deck();
            for (int i = CardCanvas.this.pile.pileCards.indexOf(CardCanvas.this); i < CardCanvas.this.pile.pileCards.size(); i++)
                deck.AddCard(CardCanvas.this.pile.pileCards.get(i).GetCard());

            Transferable transferable = new DeckTransferable(deck);

            System.out.println("Drag started");
            try
            {
                e.startDrag(DragSource.DefaultCopyNoDrop, transferable, CardCanvas.this.dragSourceListener);
            }
            catch (InvalidDnDOperationException exc)
            {
                System.err.println(exc);
            }
        }
    }

    class CardCanvasDragSourceListener implements DragSourceListener
    {
        public void dragDropEnd(DragSourceDropEvent e)
        {
            if (e.getDropSuccess() == false)
            {
                System.out.println("Drag not successful :(, CardCanvasDragSourceListener.dragDropEnd()");
                return;
            }

            if (e.getDropAction() == DnDConstants.ACTION_MOVE)
            {
                CardCanvas.this.pile.Remove(CardCanvas.this);
            }
        }

        public void dragEnter(DragSourceDragEvent e)
        {
            DragSourceContext context = e.getDragSourceContext();
            int myaction = e.getDropAction();
            if ((myaction & CardCanvas.this.dragAction) != 0 && CardCanvas.this.card.GetFacing())
                context.setCursor(DragSource.DefaultCopyDrop);
            else
                context.setCursor(DragSource.DefaultCopyNoDrop);
        }

        public void dragOver(DragSourceDragEvent e)
        {
            //
        }

        public void dragExit(DragSourceEvent e)
        {
            //
        }

        public void dropActionChanged(DragSourceDragEvent e)
        {
            DragSourceContext context = e.getDragSourceContext();
            context.setCursor(DragSource.DefaultCopyNoDrop);
        }
    }
}
